package cn.yoyo.controller.adm;

import cn.yoyo.entity.BookType;
import cn.yoyo.tools.RespJson;
import cn.yoyo.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class BookTypeController {
    @Autowired
    BookTypeService bookTypeService;

    @RequestMapping("/api/adm/type/findAll")
    public Map<String, Object> findAll() {
        return RespJson.pack(200, "", bookTypeService.findAll());
    }

    @Transactional
    @RequestMapping("/api/adm/type/add")
    public Map<String, Object> add(@RequestBody BookType bookType) {
        System.out.println(bookType);
        // bookType.getId==0?添加:修改
        if (bookType.getId() == 0) {
            if (bookTypeService.add(bookType.getType(), bookType.getParentid(), bookType.getPriority()) > 0) {
                return RespJson.pack(200, "添加成功", bookTypeService.findAll());
            }
        }
        if (bookType.getId() > 0) {
            if (bookTypeService.update(bookType.getId(), bookType.getType(), bookType.getPriority()) > 0) {
                return RespJson.pack(200, "修改成功", bookTypeService.findAll());
            }
        }
        return null;
    }


    @RequestMapping("/api/adm/type/delete/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        bookTypeService.delete(id);
        return RespJson.pack(200, "锁定成功", bookTypeService.findAll());
    }

    @RequestMapping("/api/adm/type/unlock/{id}")
    public Map<String, Object> unlock(@PathVariable("id") Integer id) {
        bookTypeService.unlock(id);
        return RespJson.pack(200, "解锁成功", bookTypeService.findAll());
    }

    @RequestMapping("/api/adm/type/findById/{id}")
    public Map<String, Object> findById(@PathVariable("id") Integer id) {
        return RespJson.pack(200, "", bookTypeService.findById(id));
    }
}
